package com.muy.redis.view.window.redis.view.table;

import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.ui.RoundedLineBorder;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.JBUI;
import com.muy.redis.utils.GuiUtils;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @Author jiyanghuang
 * @Date 2022/8/28 00:12
 */
public class CellEditorTextV2 extends AbstractCellEditor implements TableCellEditor, TreeCellEditor {

    private final JPanel mainPanel;
    private final JTextField textField;
    private final JLabel button;
    Border BUTTON_OUTSIDE_BORDER = JBUI.Borders.empty(1);
    Border BUTTON_INSIDE_BORDER = JBUI.Borders.empty(0, 8);
    Color BUTTON_BORDER_COLOR = new JBColor(new Color(0x8C8C8C), new Color(0x606060));
    RoundedLineBorder BUTTON_LINE_BORDER = new RoundedLineBorder(BUTTON_BORDER_COLOR, 4);
    CompoundBorder BUTTON_BORDER = new CompoundBorder(BUTTON_OUTSIDE_BORDER, new CompoundBorder(BUTTON_LINE_BORDER, BUTTON_INSIDE_BORDER));

    private Project project;
    private boolean[] editorStatus = {true};

    public CellEditorTextV2(Project project){

        this.project = project;
        mainPanel = new JPanel();
        // 一定要设置 borderLayout, 否则框不会匹配
        mainPanel.setLayout(new BorderLayout(0, 0));
        mainPanel.setBounds(0, 0, 0, 0);
//        mainPanel.withBorder(JBUI.Borders.empty(0));
        textField = new JBTextField();
        textField.setMargin(JBUI.insets(0, 0, 0, 0));
        mainPanel.add(textField, BorderLayout.CENTER);

        button = new JLabel(GuiUtils.DATA_EDITOR_BROWSE);
        button.setBorder(BUTTON_BORDER);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                com.muy.common.dialog.DialogJson dialogJson = new com.muy.common.dialog.DialogJson(project, (text) -> {
                    editorStatus[0] = true;
                    textField.setText(text);
                }, textField.getText());
                editorStatus[0] = false;
                dialogJson.show();
            }
        });
//        textField.setPreferredSize(new Dimension(150, -1));
        textField.setEditable(false);
        String shortcutText = "this is Value";
        button.setToolTipText("Open editor (" + shortcutText + ')');
        mainPanel.add(button, BorderLayout.EAST);
    }

    /**
     * 如果使用了 ColumnDesc 进行包装的话，可以在内部定义如何取值和更新就可以了
     * @param table
     * @param value  想办法把 value 记录下来，然后在确认后进行值的更新，列表再获取时，其实值也就被更新了
     * @param isSelected
     * @param row
     * @param column
     * @return
     */
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//        System.out.println("value-->" + value);
        if(value instanceof ColumnDesc){
            ColumnDesc columnDesc = (ColumnDesc)value;
            textField.setText(String.valueOf(columnDesc.getValueShow()));
        }
//        System.out.println("getCellEditorValue-->");
        return mainPanel;
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        return null;
    }

    /**
     * 这个值被返回到哪了？
     * 这个值并不一定被用到，取决于 CellEditor 本身是如何实现的
     * @return
     */
    @Override
    public Object getCellEditorValue() {
//        System.out.println("getCellEditorValue-->");
        return textField.getText();
    }
}
